package com.dyava.commons.clock;

import com.dyava.commons.lock.KeyLockPool;
import com.dyava.commons.lock.local.LocalKeyLockPool;
import com.dyava.commons.clock.mysql.MySqlClockSender;
import com.dyava.commons.clock.mysql.MysqlClockReceiver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

/**
 * Created by jjliu on 2020/4/16.
 */
@Configuration
public class Configure {

    @Bean
    DemoHandle demoHandle(){
        return new DemoHandle();
    }

    @Bean
    DataSource dataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/sscrm?serverTimezone=GMT%2B8");
        dataSource.setUsername("root");
        dataSource.setPassword("haosql");
        return dataSource;
    }

    @Bean
    JdbcTemplate jdbcTemplate(DataSource dataSource){
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        return jdbcTemplate;
    }

    @Bean
    KeyLockPool keyLockPool(){
        LocalKeyLockPool keyLockPool = new LocalKeyLockPool();
        return keyLockPool;
    }

    @Bean
    MysqlClockReceiver mysqlClockReceiver(JdbcTemplate jdbcTemplate,KeyLockPool keyLockPool,DemoHandle demoHandle){
        MysqlClockReceiver mysqlClockReceiver = new MysqlClockReceiver();
        mysqlClockReceiver.setKeyLockPool(keyLockPool);
        mysqlClockReceiver.setJdbcTemplate(jdbcTemplate);
        mysqlClockReceiver.getClockHandleList().add(demoHandle);
        return mysqlClockReceiver;
    }

    @Bean
    MySqlClockSender mySqlClockSender(JdbcTemplate jdbcTemplate){
        MySqlClockSender mySqlClockSender =  new MySqlClockSender();
        mySqlClockSender.setJdbcTemplate(jdbcTemplate);
        return mySqlClockSender;
    }

}
